package com.freestyle.multidatasource.ext;

import com.freestyle.multidatasource.DataSourceContextHolder;
import com.freestyle.multidatasource.annotation.TargetDataSource;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
 * Created by rocklee on 2021/12/3 17:12
 */
public class TransactionInterceptorEx extends TransactionInterceptor {
  @Override
  public Object invoke(MethodInvocation invocation) throws Throwable {
      TargetDataSource ts=invocation.getMethod().getAnnotation(TargetDataSource.class);
      if (ts!=null){
         DataSourceContextHolder.changeDataSource(ts.value());
      }
      return super.invoke(invocation);
  }

  public TransactionInterceptorEx(PlatformTransactionManager ptm, TransactionAttributeSource tas) {
    super(ptm, tas);
  }
}
